Try-Catch Statement
Pluto provides a more intuitive way to handle errors than pcall
and xpcall
.
pluto
local function get_fruit_rating(fruit)return switch fruit docase "apple" -> 8case "banana" -> 7case "orange" -> 9case "mango" -> 10case "grape" -> 6case "strawberry" -> 9case "cucumber", "tomato" -> error("What are you, a botanist?")endendlocal function try_get_fruit_rating(fruit)tryreturn get_fruit_rating(fruit)catch e thenif e:find("What are you, a botanist?") thenreturn -10endendendprint(try_get_fruit_rating("apple")) --> 8print(try_get_fruit_rating("cucumber")) --> -10print(try_get_fruit_rating("car")) --> nil
Using Compatibility Mode?
You may need to use pluto_try
and pluto_catch
instead. Alternatively, pluto_use try, catch
will enable both keywords independently of the environment settings.